package com.lab;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;



public class ReadData {
	  private File file2;
	  private List<Sity> listOfSities = new ArrayList<Sity>();
	  
	public void readData() {
		 JFileChooser fileopen = new JFileChooser();       
		 fileopen.setDialogTitle("Відкрити файл з назвами міст");
         int ret = fileopen.showDialog(null, "Відкрити файл");                
         if (ret == JFileChooser.APPROVE_OPTION) {
             file2 = fileopen.getSelectedFile();
            // label.setText(file.getName());
         } else if (JFileChooser.CANCEL_SELECTION != null) {
        	 System.out.println("Skasovano");
        	 file2= new File("C:\\Users\\MUSTANG\\Desktop\\міста2.txt");
        	 }
     
	    try {
	    	//file2.
	      FileReader file = new FileReader(file2.getAbsolutePath());
	    	//if (file2.exists()) System.out.println(file2.getAbsolutePath());
	     
	      BufferedReader buff = new BufferedReader(file);
	      String line = buff.readLine();
	      int i=0;
	      while (line != null) {
	    	StringTokenizer tok = new StringTokenizer(line, ",");
	        String name = tok.nextToken();
	        int x = Integer.valueOf(tok.nextToken()).intValue();
	        int y = Integer.valueOf(tok.nextToken()).intValue();
	        Sity one= new Sity(name, x, y);
	       //allNewEdges.add(one);
	        //table.put(i, from);  // Update priority queue
	       listOfSities.add(i, one); 
	      // System.out.println(i+" Місто: "+name + ": x="+x+", y="+y+".");
	        line = buff.readLine();
	        i++;
	      }
	      buff.close();
	    } catch (IOException e) {
	      //
	    }
	  }
	
	public void readData(String path) {
		 try {
	    	//file2.
	      FileReader file = new FileReader(path);
	    	//if (file2.exists()) System.out.println(file2.getAbsolutePath());
	     
	      BufferedReader buff = new BufferedReader(file);
	      String line = buff.readLine();
	      int i=0;
	      while (line != null) {
	    	StringTokenizer tok = new StringTokenizer(line, ",");
	        String name = tok.nextToken();
	        int x = Integer.valueOf(tok.nextToken()).intValue();
	        int y = Integer.valueOf(tok.nextToken()).intValue();
	        Sity one= new Sity(name, x, y);
	       //allNewEdges.add(one);
	        //table.put(i, from);  // Update priority queue
	       listOfSities.add(i, one); 
	      // System.out.println(i+" Місто: "+name + ": x="+x+", y="+y+".");
	        line = buff.readLine();
	        i++;
	      }
	      buff.close();
	    } catch (IOException e) {
	      //
	    }
	  }
	
	
	public List<Sity> getListOfSities() {
		return this.listOfSities;
	}
	
	
}
class Sity{
	private String sity;
	private int x;
	private int y;
	
	public Sity(String sity, int x, int y){
		this.sity=sity;
		this.x=x;
		this.y=y;
	}
	
	public Sity() {
		// TODO Auto-generated constructor stub
		this.sity="";
		this.x=0;
		this.y=0;
	}

	public String getNameofSity(){
		return this.sity;
	}
	public int getCordXofSity(){
		return this.x;
	}
	public int getCordYofSity(){
		return this.y;
	}

}